package org.toonan.rental.chart;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ThreadChartActivity extends Activity implements View.OnClickListener,
		AdapterView.OnItemClickListener
{
	ThreadChartAdapter adapter;
	private Animation animation;
	float[] increased;
	ListView listview;
	private PointView[] pointViews = new PointView[7];
	HorizontalScrollView scrollview;
	private int selected = -1;
	private LinearLayout threadLayout;
	private ThreadView[] views2 = new ThreadView[6];
	private String[] xScale = { "一月", "二月", "三月", "四月", "五月", "六月", "七月" };
	private float[] yScale = { 60.0F, 80.0F, 50.0F, 30.0F, 10.0F, 40.0F, 80.0F };
	
	private Handler myHandler = new Handler()
	{
		public void handleMessage(Message paramMessage)
		{
			Animation localAnimation = AnimationUtils.loadAnimation(ThreadChartActivity.this,
					R.anim.line_anim);
			int i = paramMessage.what;
			ThreadChartActivity.this.threadLayout.removeAllViews();
			ThreadChartActivity.this.threadLayout.addView(ThreadChartActivity.this.views2[i],
					new ViewGroup.LayoutParams(80, -2));
			ThreadChartActivity.this.views2[i].setAnimation(localAnimation);
		}
	};
	
	protected void onCreate(Bundle paramBundle)
	{
		super.onCreate(paramBundle);
		setContentView(R.layout.thread_chart);
		initButton();
		createChartlineView();
		createThreadChartView();
		initListView();
	}
	
	private void createChartlineView()
	{
		((LinearLayout) findViewById(R.id.chartline2)).addView(new ChartLineView(this),
				new ViewGroup.LayoutParams(740, 350));
	}
	
	private void createThreadChartView()
	{
		LinearLayout localLinearLayout = (LinearLayout) findViewById(R.id.pointLayout);
		threadLayout = ((LinearLayout) findViewById(R.id.threadLayout));
		scrollview = ((HorizontalScrollView) findViewById(R.id.threadscrollview));
		int i = 0;
		int j = 0;
		for (int k = 0; k < 6; k++)
		{
			new Thread()
			{
				public void run()
				{
					int i = 0;
					if (i >= ThreadChartActivity.this.views2.length)
						return;
					try
					{
						Message localMessage = ThreadChartActivity.this.myHandler.obtainMessage();
						localMessage.what = i;
						ThreadChartActivity.this.myHandler.sendMessage(localMessage);
						Thread.sleep(300);
						i++;
					}
					catch (InterruptedException localInterruptedException)
					{
						localInterruptedException.printStackTrace();
					}
				}
			}.start();
			pointViews[i] = new PointView(this, yScale[i], xScale[i]);
			i++;
			if (j < pointViews.length)
			{
				localLinearLayout.addView(this.pointViews[j], new ViewGroup.LayoutParams(80, -2));
				pointViews[j].setId(j);
				pointViews[j].setOnClickListener(this);
				j++;
			}
			this.views2[k] = new ThreadView(this, this.yScale[(k + 1)], this.yScale[k]);
		}
	}
	
	private void initButton()
	{
		int i = 0;
		Button[] arrayOfButton = { (Button) findViewById(R.id.button1), (Button) findViewById(R.id.button2),
				(Button) findViewById(R.id.button3), (Button) findViewById(R.id.button4) };
		ButtonListener localButtonListener = new ButtonListener(this);
		int j = arrayOfButton.length;
		while (true)
		{
			if (i >= j)
				return;
			arrayOfButton[i].setOnClickListener(localButtonListener);
			i++;
		}
	}
	
	private void initListView()
	{
		listview = ((ListView) findViewById(2131230757));
		increased = new float[yScale.length];
		increased[0] = 0.0F;
		for (int i = 1;; i++)
		{
			if (i >= this.yScale.length)
			{
				adapter = new ThreadChartAdapter(this);
				listview.setAdapter(adapter);
				animation = AnimationUtils.loadAnimation(this, R.anim.bar_anim);
				listview.setOnItemClickListener(this);
				return;
			}
			this.increased[i] = (this.yScale[i] - this.yScale[(i - 1)]);
		}
	}
	
	private void invalidatePointView(int paramInt)
	{
		PointView[] arrayOfPointView = this.pointViews;
		int i = arrayOfPointView.length;
		for (int j = 0;; j++)
		{
			if (j >= i)
			{
				this.pointViews[paramInt].setPointColor(-65536);
				this.pointViews[paramInt].invalidate();
				return;
			}
			PointView localPointView = arrayOfPointView[j];
			localPointView.setPointColor(-256);
			localPointView.invalidate();
		}
	}
	
	public void onClick(View paramView)
	{
		selected = paramView.getId();
		invalidatePointView(this.selected);
		listview.setAdapter(this.adapter);
		listview.smoothScrollToPosition(this.selected);
		pointViews[this.selected].startAnimation(this.animation);
	}
	
	public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong)
	{
		pointViews[paramInt].startAnimation(this.animation);
		selected = paramInt;
		invalidatePointView(selected);
		listview.setAdapter(adapter);
		listview.smoothScrollToPosition(selected);
		scrollview.scrollTo(20 * selected, 0);
	}
	
	private class ThreadChartAdapter extends BaseAdapter
	{
		Context context;
		
		public ThreadChartAdapter(Context arg2)
		{
			this.context = arg2;
		}
		
		public int getCount()
		{
			return ThreadChartActivity.this.yScale.length;
		}
		
		public Object getItem(int paramInt)
		{
			return Integer.valueOf(paramInt);
		}
		
		public long getItemId(int paramInt)
		{
			return paramInt;
		}
		
		public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
		{
			final views localviews;
			System.out.println("paramView=" + paramView);
			if (paramView == null)
			{
				paramView = (View) LayoutInflater.from(context).inflate(R.layout.threadchart_item, null);
				localviews = new views();
				localviews.textview1 = ((TextView) paramView.findViewById(R.id.textView1));
				localviews.textview2 = ((TextView) paramView.findViewById(R.id.textView2));
				localviews.textview3 = ((TextView) paramView.findViewById(R.id.textView3));
				paramView.setTag(localviews);
			}
			else
			{
				localviews = (views) paramView.getTag();
			}
			
			localviews.textview1.setText(ThreadChartActivity.this.xScale[paramInt]);
			localviews.textview2.setText("" + ThreadChartActivity.this.yScale[paramInt]);
			localviews.textview3.setText("" + ThreadChartActivity.this.increased[paramInt]);
			if (ThreadChartActivity.this.selected == paramInt)
				paramView.setBackgroundResource(R.drawable.item_selection);
			return paramView;
		}
		
		class views
		{
			TextView textview1;
			TextView textview2;
			TextView textview3;
			
			views()
			{
			}
		}
	}
}